home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
CD ROM Paradise Collection 4
/
CD ROM Paradise Collection 4 1995 Nov.iso
/
program
/
4th86_v4.zip
/
SHARPS.4TH
< prev
next >
Wrap
Text File
|
1994-01-01
|
6KB
|
153 lines
( forget strt
: strt ; )
off printload
unsplit
( ************************************************* )
( * double precision formatted output routines. * )
( * file sharps.4th December 2nd. 1993 * )
( * these programs are modeled after the fig-forth* )
( * routines that perform the same function. * )
( * and are adapted from the original CP/M80 * )
( * source code * )
( ************************************************* )
30 ( size of string buffer containing the number)
dup block #buffer ( string buffer)
'' #buffer + const #endbuf ( address of end )
2 block #inpoint ( input pointer)
1 block #sign ( sign flag)
: #putbyte ( put the byte at tos in the buffer)
#inpoint @ 1 - ( new input pointer)
dup #buffer =
if ( do we have overflow?)
│ #endbuf 1 - swap
│ do
│ │ "*" i b!
│ loop ( fill buffer with stars.)
│ drop ( discard the character)
else ( if no overflow, update pointer and store char.)
│ dup #inpoint ! b!
then
; ( end of #putbyte.)
: <# ( begin a format operation)
#endbuf #inpoint ! ( input pointer is past end of buffer.)
,dup ,0 ,<
if ( test the sign of the number)
│ ,-1* ( negate the number)
│ 1 ( sign=1)
else
│ 0 ( sign=0)
then
#sign b! ( save the sign)
; ( end of <# )
: # ( convert one digit into the buffer)
,10 ,/mod ,swap single ( convert the digit)
"0" + ( make it ascii)
#putbyte ( put it in the buffer)
; ( end of # )
: #s ( convert the digits until ,tos is zero)
begin ( do until tos=0 )
│ # ( convert a digit)
│ ,dup ,0= ( test tos)
end ; ( end of #s )
: #. ( put in a decimal point and convert the
2
rest of the digits)
"." #putbyte #s ; ( end of #. )
: #- ( put in an optional sign field )
#sign b@
if
│ "-" #putbyte
then ;
: #+ ( put in a required sign field )
#sign b@ if "-" else "+" then #putbyte ;
: #> ( terminate the formatting operation, return the addr.)
( of the beginning of the string representing the )
( field. on entry, tos=the field width, ,nos is ,0 )
#endbuf #inpoint @ - 1 + ( current field size + 1.)
do ( note: field size<current makes no spaces )
│ 20h #putbyte
loop ( field is now correct width)
,drop ( discard the number being converted)
#endbuf #inpoint @ - ( length of string)
#inpoint @ 1 - swap over b! ( store in string)
( note: tos points to the length byte of the string)
; ( end of #> )
( ----------------------------------------------------- )
( 5 JUNE 1982 Donald M. Ramsey )
( This extension [ original June 1982 by Donald M. Ramsey ] demonstrates the
use of the words in file SHARPS.4TH for printing single and double precision
numbers in a specified field width with and without sign. )
2 block fw ( VARIABLE FIELD WIDTH WILL BE USED)
7 fw ! ( use field width of seven for now)
: s.# ( single precision number without decimal pt or sign)
double ( convert number to double precision)
<# #S FW @ #> ( convert number to string within #BUFFER)
." ( print contents of #BUFFER)
; ( ----------------------------- )
: d.# ( double precision number w/o decimal pt or sign)
<# #S FW @ #> ."
; ( ----------------------------- )
: s2.# ( single precision with 2 decimal places and forced sign)
double
<# # # #. #S #+ FW @ #> ."
( note that the field specifier <#...#> works backwards, ie.
you specify output starting with right end of number)
; ( ----------------------------- )
: D3.# ( double precision with 3 decimal places and optional sign)
<# # # # #. #S #- FW @ #> ."
; ( ----------------------------- )
: DN.# ( double prec. with field width passed on stack)
<# #S #- 3 pick #> ." drop
; ( ----------------------------- )
cls
" print a single precision number rt justified in seven char field " ."
crlf 123 S.# " <= 123 S.# " ." crlf
crlf " print a double precision number " ."
crlf 12345, D.# " <= 12345, D.# " ." crlf
crlf " print a single precision number with 2 places to rt of decimal pt " ."
crlf " put a sign in number even if positive " ."
crlf 123 S2.# " <= 123 S2.# " ."
crlf -123 S2.# " <= -123 S2.# " ." crlf
crlf " print a double precision number with 3 places to rt " ."
crlf " put a sign in number if it is negative " ."
crlf 1234, D3.# " <= 1234, D3.# " ."
crlf -1234, D3.# " <= -1234, D3.# " ." crlf
crlf " print a double prec. number with field width = n " ." crlf
crlf 10 123, DN.# " <= 10 123, DN.# ( n = 10 ) " ."
crlf 5 -7154, DN.# " <= 5 -7154, DN.# ( n = 5 ) " ."
crlf
( change FW to other field widths and try experimenting! )